﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Linq;

namespace VCommons
{
    /// <summary>
    /// Xml操作
    /// </summary>
    public class PhoneTemplate
    {
        private string docName = "";
        public PhoneTemplate()
        {
            docName = HttpContext.Current.Server.MapPath("/Configs/CodeTemplate.xml");//配置文件路径 
        }


        /// <summary> 
        /// 加载Config文件 
        /// </summary> 
        /// <param name="path"></param> 
        /// <returns></returns> 
        public XDocument Load()
        {
            FileInfo file = new FileInfo(docName);
            file.IsReadOnly = false;
            return XDocument.Load(docName);
        }

        /// <summary>
        /// 添加模板
        /// </summary>
        /// <param name="templateName">模板名称</param>
        /// <param name="templateValue">模板内容</param>
        /// <returns>-1：异常错误，0：已存在，1：添加成功</returns>
        public int Add(string templateName, string templateValue)
        {
            XDocument xDoc = Load();
            try
            {
                var query = from p in xDoc.Descendants("PhoneTemplate") where p.Element("TemplateName").Value == templateName select p;
                if (query.Count() > 0)
                {
                    return 0;
                }
                xDoc.Element("Template").Add(new XElement("PhoneTemplate",
              new XElement("TemplateName", templateName),
              new XElement("TemplateValue", templateValue)));
                xDoc.Save(docName);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;

            }

        }


        /// <summary> 
        /// 删除 
        /// </summary> 
        /// <param name="templateName">模板名称</param>    
        /// <returns>-1：异常错误，0：不存在，1：成功</returns> 
        public int Remove(string templateName)
        {
            try
            {
                XDocument xDoc = Load();
                var query = from p in xDoc.Descendants("PhoneTemplate")
                            where p.Element("TemplateName").Value == templateName
                            select p;
                if (query.Count() <= 0)
                {
                    return 0;
                }
                else
                {
                    query.Remove();
                    xDoc.Save(docName);
                    return 1;
                }

            }
            catch (Exception ex)
            {
                return -1;

            }
        }

        /// <summary>
        /// 更新模板
        /// </summary>
        /// <param name="templateName">模板名称</param>
        /// <param name="templateValue">模板内容</param>
        /// <returns>-1：异常错误，0：已存在，1：添加成功</returns>
        public int ModifyTemplate(string templateName, string templateValue)
        {
            try
            {
                XDocument xDoc = Load();
                var query = from p in xDoc.Descendants("PhoneTemplate")
                            where p.Element("TemplateName").Value == templateName
                            select p;
                var currentElement = query.FirstOrDefault();
                if (currentElement != null)
                {
                    currentElement.Element("TemplateName").Value = templateName;
                    currentElement.Element("TemplateValue").Value = templateValue;
                    xDoc.Save(docName);
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return -1;
            }

        }

        /// <summary>
        /// 获取单个模板
        /// </summary>
        /// <param name="templateName">模板名称</param>     
        /// <returns></returns>
        public PhoneTemplateModel GetPhoneCode(string templateName)
        {
            XDocument xDoc = Load();
            var query = from p in xDoc.Descendants("PhoneTemplate")
                        where p.Element("TemplateName").Value == templateName
                        select new PhoneTemplateModel
                        {
                            TemplateName = p.Element("TemplateName").Value,
                            TemplateValue = p.Element("TemplateValue").Value
                        };

            return query.FirstOrDefault();
        }

        /// <summary>
        /// 获取所有模板
        /// </summary>
        /// <returns></returns>
        public IList<PhoneTemplateModel> GetPhoneCodeList()
        {
            XDocument xDoc = Load();
            var query = from p in xDoc.Descendants("PhoneTemplate")
                        select new PhoneTemplateModel
                        {
                            TemplateName = p.Element("TemplateName").Value,
                            TemplateValue = p.Element("TemplateValue").Value
                        };

            return query.ToList();
        }
    }

    /// <summary>
    /// 模板属性
    /// </summary>
    public class PhoneTemplateModel
    {
        public PhoneTemplateModel() { }
        private string templateName = string.Empty;

        public string TemplateName
        {
            get { return templateName; }
            set { templateName = value; }
        }
        private string templateValue = string.Empty;

        public string TemplateValue
        {
            get { return templateValue; }
            set { templateValue = value; }
        }
    }
}
